﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Text;
using System.Threading.Tasks;

namespace DirectSell.Common.Http
{

    public class HttpHelper
    {
        public static string Post(string Msg, string Url)
        {
            string result;
            try
            {
                byte[] bytes = Encoding.GetEncoding("UTF-8").GetBytes(Msg);
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(Url);
                httpWebRequest.Method = "POST";
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.ContentLength = (long)bytes.Length;
                using (Stream requestStream = httpWebRequest.GetRequestStream())
                {
                    requestStream.Write(bytes, 0, bytes.Length);
                    HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                    using (StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.GetEncoding("UTF-8")))
                    {
                        result = streamReader.ReadToEnd();
                    }
                }
            }
            catch (Exception var_6_9D)
            {
                result = "";
            }
            return result;
        }

        public static string PostX(string Msg, string Url)
        {
            string result;
            try
            {
                byte[] bytes = Encoding.GetEncoding("UTF-8").GetBytes(Msg);
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(Url);
                httpWebRequest.Method = "POST";
                httpWebRequest.ContentType = "application/x-www-form-urlencoded";
                httpWebRequest.ContentLength = (long)bytes.Length;
                Stream requestStream = httpWebRequest.GetRequestStream();
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Close();
                HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
                string text = streamReader.ReadToEnd();
                result = text;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
            }
            return result;
        }

        public static string Get(string Url)
        {
            string result;
            try
            {
                string text = "";
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(Url);
                HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                Stream responseStream = httpWebResponse.GetResponseStream();
                try
                {
                    using (StreamReader streamReader = new StreamReader(responseStream))
                    {
                        text = streamReader.ReadToEnd();
                    }
                }
                finally
                {
                    responseStream.Close();
                }
                result = text;
            }
            catch (Exception var_6_5B)
            {
                result = "";
            }
            return result;
        }

        public static string GetString(string url, Hashtable param = null)
        {
            bool flag = param != null;
            if (flag)
            {
                int num = 0;
                StringBuilder stringBuilder = new StringBuilder();
                foreach (DictionaryEntry dictionaryEntry in param)
                {
                    bool flag2 = num == 0;
                    if (flag2)
                    {
                        stringBuilder.AppendFormat("?{0}={1}", dictionaryEntry.Key, dictionaryEntry.Value);
                        num++;
                    }
                    else
                    {
                        stringBuilder.AppendFormat("&{0}={1}", dictionaryEntry.Key, dictionaryEntry.Value);
                    }
                }
                url += stringBuilder.ToString();
            }
            string result = string.Empty;
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            Stream responseStream = httpWebResponse.GetResponseStream();
            try
            {
                using (StreamReader streamReader = new StreamReader(responseStream))
                {
                    result = streamReader.ReadToEnd();
                }
            }
            finally
            {
                responseStream.Close();
            }
            return result;
        }
    }
}
